//
//  CouponSelectionView.swift
//  CMB250716
//
//  Created by Assistant on 2024-12-19.
//

import SwiftUI

struct CouponSelectionView: View {
    @Binding var selectedCoupon: Coupon?
    @EnvironmentObject var dataManager: DataManager
    @Environment(\.dismiss) private var dismiss
    
    var body: some View {
        NavigationView {
            VStack(spacing: 0) {
                // 标题区域
                VStack(spacing: 8) {
                    Text(dataManager.currentLanguage == "zh" ? "选择优惠券" : "Select Coupon")
                        .font(.title2)
                        .fontWeight(.bold)
                        .foregroundColor(.white)
                    
                    Text(dataManager.currentLanguage == "zh" ? "选择一张优惠券应用到您的订单" : "Choose a coupon to apply to your order")
                        .font(.subheadline)
                        .foregroundColor(.gray)
                        .multilineTextAlignment(.center)
                }
                .padding(.top, 20)
                .padding(.horizontal, 20)
                
                // 优惠券列表
                ScrollView {
                    LazyVStack(spacing: CouponSelectionConfig.CardDimensions.cardSpacing) {
                        ForEach(dataManager.availableCoupons.filter { 
                            CouponSelectionConfig.shouldShowCoupon($0, dataManager: dataManager, isCartSelection: true) 
                        }) { coupon in
                            CouponRowView(
                                coupon: coupon,
                                isSelected: selectedCoupon?.id == coupon.id,
                                onTap: {
                                    selectedCoupon = coupon
                                    dismiss()
                                }
                            )
                            .environmentObject(dataManager)
                        }
                    }
                    .padding(.horizontal, 20)
                }
                .padding(.top, 20)
            }
            .background(
                LinearGradient(
                    colors: [
                        Color(red: 0.0, green: 0.0, blue: 0.0),
                        Color(red: 0.05, green: 0.02, blue: 0.08),
                        Color(red: 0.08, green: 0.04, blue: 0.12),
                        Color(red: 0.12, green: 0.06, blue: 0.15),
                        Color(red: 0.15, green: 0.08, blue: 0.18),
                        Color(red: 0.18, green: 0.10, blue: 0.20),
                        Color(red: 0.20, green: 0.12, blue: 0.22),
                        Color(red: 0.22, green: 0.14, blue: 0.25)
                    ],
                    startPoint: .topLeading,
                    endPoint: .bottomTrailing
                )
                .ignoresSafeArea()
            )
            .preferredColorScheme(.dark)
            .navigationBarTitleDisplayMode(.inline)
            .toolbar {
                ToolbarItem(placement: .navigationBarTrailing) {
                    Button(dataManager.currentLanguage == "zh" ? "取消" : "Cancel") {
                        dismiss()
                    }
                    .foregroundColor(.white)
                }
            }
        }
    }
}

#Preview {
    CouponSelectionView(selectedCoupon: .constant(nil))
        .environmentObject(DataManager.shared)
}